package com.runfast.waimai.web.controller.gallery;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.PageInfo;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.RunfastGallery;
import com.runfast.waimai.dao.model.RunfastGalleryExample;
import com.runfast.waimai.service.RunfastGalleryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: lijin
 * @date: 2018年08月15日
 */
@RestController(value = "galleryController")
@RequestMapping(value = "/server/gallery")
public class GalleryController {

    @Autowired
    private RunfastGalleryService galleryService;

    /**
     * 图库列表
     * @param pageable
     * @param categoryid
     * @return
     */
    @GetMapping(value = "/list",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> list(@PageableDefault Pageable pageable,Integer categoryid){

        BaseUser admin = SessionContext.getAdmin();

        RunfastGalleryExample example = new RunfastGalleryExample();
        RunfastGalleryExample.Criteria criteria = example.createCriteria().andDelEqualTo(false);
        if(categoryid != null){
            criteria.andCategoryidEqualTo(categoryid);
        }
        if(admin == null || admin.getType() != 1){ //非后台管理人员
            criteria.andEnableEqualTo(true);
        }
        int count = galleryService.countByExample(example);
        List<RunfastGallery> galleries = null;
        if(pageable.getOffset() < count -1){
            galleries = galleryService.selectByExampleWithPageable(example, pageable);
        }else{
            galleries = new ArrayList<>();
        }
        return Result.ok("",new PageInfo<RunfastGallery>(count, pageable.getPageNumber(), pageable.getPageSize(), galleries));
    }



    /**
     * 添加图库图片
     * @return
     */
    @PostMapping(value = "/save",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> save(@Validated( value = {RunfastGallery.Insert.class}) RunfastGallery gallery, BindingResult result){
        BaseUser admin = SessionContext.getAdmin();
        if(admin != null && admin.getType() == 1) {
            if (result.hasErrors()) {
                return Result.fail(null, result.getFieldError().getDefaultMessage());
            }
            if(galleryService.insertSelective(gallery) > 0){
                return Result.ok("ok");
            }
            return Result.fail(ResultCode.FAIL);
        }
        return Result.fail(null, "权限不足");
    }

    /**
     * 修改图片
     * @param gallery
     * @param result
     * @return
     */
    @PostMapping(value = "/modify", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> modify(@Validated( value = {RunfastGallery.Modify.class}) RunfastGallery gallery, BindingResult result){
        BaseUser admin = SessionContext.getAdmin();
        if(admin != null && admin.getType() == 1) {
            if (result.hasErrors()) {
                return Result.fail(null, result.getFieldError().getDefaultMessage());
            }
            RunfastGalleryExample example = new RunfastGalleryExample();
            example.createCriteria().andIdEqualTo(gallery.getId()).andDelEqualTo(false);
            if(galleryService.updateByExampleSelective(gallery, example) > 0){
                return Result.ok("ok");
            }
            return Result.fail(ResultCode.FAIL);
        }
        return Result.fail(null, "权限不足");
    }

    /**
     * 删除图库
     * @param id
     * @return
     */
    @PostMapping(value = "/del")
    public Result<?> delete(Integer id){
        BaseUser admin = SessionContext.getAdmin();
        if(admin != null && admin.getType() == 1) {
            if (id == null) {
                return Result.fail(null, "参入参数错误");
            }
            RunfastGallery gallery = new RunfastGallery();
            gallery.setId(id);
            gallery.setDel(true);
            int count = galleryService.updateByPrimaryKeySelective(gallery);
            if (count > 0) {
                return Result.ok("ok");
            }
            return Result.fail(ResultCode.FAIL, "删除失败");
        }
        return Result.fail(null, "权限不足");
    }


}
